<?php
class Grades extends Controller {


        /**
         * Grades class constructor.
         */
	function Grades()
	{
		parent::Controller();

                //Send those not logged in back to the home page
                if(!$this->session->userdata('username'))
                   redirect(base_url());
	}

        /**
         * Index page method for selecting a grade to add or edit.
         */
	function index()
	{
                $this->load->model('UserModel');
                
                $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));
                $data['fname'] = $userData['fname'];

                $data['content'] = 'grades_main_content';
                
                $this->load->view('loggedin_template', $data);
	}

        /**
         * Student selection for adding grades method.
         */
        function addselectstudent()
        {
                $this->load->model('UserModel');
                $this->load->model('StudentModel');

                $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));
                $data['fname'] = $userData['fname'];
                $data['content'] = 'grades_select_student';
                $studentData = $this->StudentModel->getStudentsInfoByUsername($this->session->userdata('username'));

                $data['student_dropdown'] = array();
                $data['student_dropdown']['null'] = 'Please select a student...';

                foreach($studentData as $i)
                {
                    $data['student_dropdown'][$i['pk']] = $i['fname'] . ' ' . $i['lname'] . '  -> Grade ' . $i['gradelevel'];
                }

                $this->load->view('loggedin_template', $data);
            
        }

        /**
         * Method for adding grades.
         */
        function addgrades()
        {

            //Load all three models
            $this->load->model('UserModel');
            $this->load->model('StudentModel');
            $this->load->model('GradeModel');

            //Get the student's info by the primary key POSTed
            $studentData = $this->StudentModel->getStudentInfoByPK($this->input->post('students'), $this->session->userdata('username'));

            //Load the user model the set the user's info from the session data
            $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));
            $data['fname'] = $userData['fname'];
            $data['students'] = $this->input->post('students');

            //Set the view's student name and content variables.
            $data['content'] = 'grades_addgrades';
            $data['student_name'] = $studentData['fname'] . ' ' . $studentData['lname'];

                    //Load the form field attributes
                    //Set attributes for the percentscore field
                    $data['percentscore_attributes'] = array(
                                                    'name' => 'percentscore',
                                                    'maxlength' => '6',
                                                    'size' => '6'
                                                     );

                    //Set attributes for the lettergrade field
                    $data['lettergrade_attributes'] = array(
                                                    'name' => 'lettergrade',
                                                    'maxlength' => '1',
                                                    'size' => '1'
                                                     );

                    //Set attributes for the pointsawarded field
                    $data['pointsawarded_attributes'] = array(
                                                    'name' => 'pointsawarded',
                                                    'maxlength' => '6',
                                                    'size' => '6'
                                                     );

                    //Set attributes for the pointspossible field
                    $data['pointspossible_attributes'] = array(
                                                    'name' => 'pointspossible',
                                                    'maxlength' => '6',
                                                    'size' => '6'
                                                     );

            //If the primary key sent in POST is null or does not pass validation return to sender
            if($this->input->post('students') == 'null')
            {
                redirect('grades');
            }
            //If this is not a postback, validate student pk and show the form
            else if(!$this->input->post('postback'))
            {
                //Validate the POST data
                $this->load->library('form_validation');
                $this->form_validation->set_rules('students', 'Student', 'trim|required');

                //If this validation fails for some odd reason, send them back
                if($this->form_validation->run() == FALSE)
                redirect('grades');

                $this->load->view('loggedin_template', $data);
            }
            //Else, validate and enter the grade
            else
            {
                //Begin the form validation process
                $this->load->library('form_validation');
                //Forward the post data to the next form 
                $data['students'] = $this->input->post('students');

                //Set the validation rules
                $this->form_validation->set_rules('assignmentname', 'Assignment Name', 'trim|required|max_length[80]');
                $this->form_validation->set_rules('subject', 'Subject', 'trim|required|max_length[30]');
                $this->form_validation->set_rules('percentscore', 'Percent Score', 'trim|max_length[6]|numeric');
                $this->form_validation->set_rules('lettergrade', 'Letter Grade', 'trim|max_length[1]|alpha');
                $this->form_validation->set_rules('pointsawarded', 'Points Awarded', 'trim|max_length[6]|numeric');
                $this->form_validation->set_rules('pointspossible', 'Points Possible', 'trim|max_length[6]|numeric');
                $this->form_validation->set_rules('notes', 'Notes', 'trim|max_length[500]');
                $this->form_validation->set_rules('students', ' ', 'required');

                //Unusual occurance.  There has been POST data tampering if it is thrown.
                $this->form_validation->set_message('students', 'There was an error with your <br /> student selection, please go back.');

                if($this->form_validation->run() == FALSE)
                {
                    $data['student_name'] = $studentData['fname'] . ' ' . $studentData['lname'];
                    $this->load->view('loggedin_template', $data);
                }
                else
                {
                     $gradeData = array();
                     $gradeData['assignmentname'] = $this->input->post('assignmentname');
                     $gradeData['subject'] = $this->input->post('subject');

                     if($this->input->post('percentscore') != '')
                        $gradeData['percentscore'] = $this->input->post('percentscore');
                     if($this->input->post('lettergrade') != '')
                        $gradeData['lettergrade'] = $this->input->post('lettergrade');
                     if($this->input->post('pointsawarded') != '')
                        $gradeData['pointsawarded'] = $this->input->post('pointsawarded');
                     if($this->input->post('pointspossible') != '')
                        $gradeData['pointspossible'] = $this->input->post('pointspossible');
                     
                     $gradeData['notes'] = $this->input->post('notes');

                     $this->GradeModel->addNewGrade($gradeData, $this->input->post('students'), $this->session->userdata('username'));

                     $data['content'] = 'grades_addsuccess';
                     $this->load->view('loggedin_template', $data);;
                }
                
            }

                
       }

       /**
        * Method for viewing the grades of all students.
        */
       function viewallstudents()
       {
           $this->load->model('UserModel');
           $this->load->model('GradeModel');

           $userData = $this->UserModel->getUserInfo($this->session->userdata('username'));
           $data['fname'] = $userData['fname'];
           $data['grade_data'] = $this->GradeModel->getGradesByUsername($this->session->userdata('username'));

           $data['content'] = 'grades_viewallstudents';
           $this->load->view('loggedin_template', $data);
                    
       }

        

}

?>
